﻿using System;
using System.Collections.Generic;
using System.Management;
using System.Text;
using System.Security.Cryptography;
using System.IO;

namespace lchealthy
{
    public class Computer
    {
        // Fields
        public string ComputerName;
        public string CpuID;
        public string DiskID;
        public string IpAddress;
        public string LoginUserName;
        public string MacAddress;
        public string SystemType;
        public string TotalPhysicalMemory;
        private static byte[] Keys = new byte[] { 0x12, 0x34, 0x56, 120, 0x90, 0xab, 0xcd, 0xef };

        // Methods
        public Computer()
        {
            this.CpuID = this.GetCpuID();
            this.MacAddress = this.GetMacAddress();
            this.DiskID = this.GetDiskID();
            this.IpAddress = this.GetIPAddress();
            this.LoginUserName = this.GetUserName();
            this.SystemType = this.GetSystemType();
            this.ComputerName = this.GetComputerName();
        }
        public static string EncryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] keys = Keys;
                byte[] buffer = Encoding.UTF8.GetBytes(encryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateEncryptor(bytes, keys), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                return Convert.ToBase64String(stream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }
        public static string DecryptDES(string encryptString, string encryptKey)
        {
            try
            {
                byte[] bytes = Encoding.UTF8.GetBytes(encryptKey.Substring(0, 8));
                byte[] keys = Keys;
                byte[] buffer = new byte[encryptString.Length];
                buffer = Convert.FromBase64String(encryptString);
                DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
                MemoryStream stream = new MemoryStream();
                CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(bytes, keys), CryptoStreamMode.Write);
                stream2.Write(buffer, 0, buffer.Length);
                stream2.FlushFinalBlock();
                return Encoding.UTF8.GetString(stream.ToArray());
            }
            catch
            {
                return encryptString;
            }
        }


        private string GetComputerName()
        {
            try
            {
                return Environment.MachineName;
            }
            catch
            {
                return "unknow";
            }
        }

        public string GetCpuID()
        {
            try
            {
                string cpuInfo = "";                
                ManagementObjectCollection moc = new ManagementClass("Win32_Processor").GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    cpuInfo = mo.Properties["ProcessorId"].Value.ToString();
                }
                moc = null;
                return cpuInfo;
            }
            catch
            {
                return "unknow";
            }
        }

        public string GetDiskID()
        {
            try
            {
                string HDid = "";
                ManagementObjectCollection moc = new ManagementClass("Win32_DiskDrive").GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    HDid = (string)mo.Properties["Model"].Value;
                }
                moc = null;
                return HDid;
            }
            catch
            {
                return "unknow";
            }
        }

        private string GetIPAddress()
        {
            try
            {
                string st = "";
                ManagementObjectCollection moc = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"])
                    {
                        Array ar = (Array)mo.Properties["IpAddress"].Value;
                        st = ar.GetValue(0).ToString();
                        break;
                    }
                }
                moc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }

        public string GetMacAddress()
        {
            try
            {
                string mac = "";
                ManagementObjectCollection moc = new ManagementClass("Win32_NetworkAdapterConfiguration").GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    if ((bool)mo["IPEnabled"])
                    {
                        mac = mo["MacAddress"].ToString();
                        break;
                    }
                }
                moc = null;
                return mac;
            }
            catch
            {
                return "unknow";
            }
        }

        private string GetSystemType()
        {
            try
            {
                string st = "";
                ManagementObjectCollection moc = new ManagementClass("Win32_ComputerSystem").GetInstances();
                foreach (ManagementObject mo in moc)
                {
                    st = mo["SystemType"].ToString();
                }
                moc = null;
                return st;
            }
            catch
            {
                return "unknow";
            }
        }

        private string GetUserName()
        {
            try
            {
                return "";
            }
            catch
            {
                return "unknow";
            }
        }
    }


}
